package com.tosh.model.comp;

import com.tosh.model.Factor;
import com.tosh.model.Match;
import com.tosh.util.Utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * User: arsentyev
 * Date: 08.10.12
 */
public class Bwin {
    public static List<Match> getMatches(String url) {
        url = "https://www.bwin.com/ru/betviewiframe.aspx?selectedLeagues=1&" + url;
        List<Match> matches = new ArrayList<Match>();
        Match match = null;
        Document doc = null;

        try {
            doc = Jsoup.connect(url).get();
        } catch (IOException e) {
            e.printStackTrace();
            return getMatches(url);
        }

        Elements trs = doc.select("table.listing tr.normal");
        Element td = null, el = null;

        for (Element tr : trs) {
            match = new Match();

            Elements tds = tr.select(".unselected");

            td = tds.get(0);
            el = td.select("td.label").get(0);
            match.setMember1(el.text());
            el = td.select("td.odd").get(0);
            match.addFactor(Factor.WIN1, Utils.tryParseFloat(el.text()));

            td = tds.get(1);
            el = td.select("td.odd").get(0);
            match.addFactor(Factor.DRAW, Utils.tryParseFloat(el.text()));

            td = tds.get(2);
            el = td.select("td.label").get(0);
            match.setMember2(el.text());
            el = td.select("td.odd").get(0);
            match.addFactor(Factor.WIN2, Utils.tryParseFloat(el.text()));

            matches.add(match);
        }

        return matches;
    }
}